package chaoyue.study.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 使用文件流进行文件拷贝
 */
public class FileCopyTest {
    public static void main(String[] args) throws Exception {
        byte[] buffer = new byte[1024 * 1024];
        FileInputStream fileInputStream = new FileInputStream("/Users/chaoyue/Downloads/归档.zip");
        FileOutputStream fileOutputStream = new FileOutputStream("/Users/chaoyue/Downloads/归档copy.zip");
        int readCount;
        int count = 0;
        int total = fileInputStream.available();
        do {
            readCount = fileInputStream.read(buffer);
            count++;
            if (readCount != -1) {
                int last = fileInputStream.available();
                System.out.println("第" + count + "次读取了" + (readCount / 1024) + "kb，剩余" + (last / 1024) + "kb，进度% " + (total - last) * 100.0 / total);
                fileOutputStream.write(buffer, 0, readCount);
                fileOutputStream.flush();
            }
        } while (readCount != -1);
        fileInputStream.close();
        fileOutputStream.close();
    }
}
